package com.qfedu.yanglao20admin.camera.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.camera.CameraList;
import com.qfedu.yanglao20.entity.camera.CameraType;
import com.qfedu.yanglao20admin.camera.dao.CameraListDao;
import com.qfedu.yanglao20admin.camera.dao.CameraTypeDao;
import com.qfedu.yanglao20admin.camera.param.CameraListParam;
import com.qfedu.yanglao20admin.camera.service.CameraListService;
import com.qfedu.yanglao20admin.camera.vo.CameraListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 摄像头列表信息业务层实现类
 */
@Service
public class CameraListServiceImpl implements CameraListService {

    @Resource
    private CameraTypeDao cameraTypeDao;
    @Resource
    private CameraListDao cameraListDao;

    @Override
    public List<CameraListVO> selectListAll() {
        return cameraListDao.selectListAll();
    }

    @Override
    public void deleteById(Integer id) {
        cameraListDao.deleteById(id);
    }

    @Override
    public Page<CameraListVO> pageCameraList(CameraListParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        //分页查询
        Page<CameraListVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<CameraListVO> cameraListVOS = cameraListDao.selectList(param);

        //页码
        page.setCurrent(param.getPageNum());
        //每页查询的记录数
        page.setSize(param.getPageSize());
        //总记录数
        Long aLong = cameraListDao.selectCount(null);
        page.setTotal(aLong);
        page.setRecords(cameraListVOS);
        return page;
    }

    @Override
    public void add(CameraListVO cameraListVO) {
        if (cameraListVO.getCameraName() == null || cameraListVO.getCameraName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写摄像头名称");
        }

        //通过前端发送的摄像头名称查询出来对应的id
        CameraType cameraType = cameraTypeDao.selectByName(cameraListVO.getCameraType());
        Integer id = cameraType.getId();

        String cameraName = cameraListVO.getCameraName();
        Integer did = cameraListVO.getDid();
        String ipAddr = cameraListVO.getIpAddr();
        String identifier = cameraListVO.getIdentifier();
        String port = cameraListVO.getPort();
        String monitorUrl = cameraListVO.getMonitorUrl();
        BigDecimal lngLat = cameraListVO.getLngLat();
        String sort = cameraListVO.getSort();

        //把相对应的数据()装到一个新实体类对象中
        CameraList cameraList = new CameraList();
        cameraList.setCameraName(cameraName);
        cameraList.setDid(did);
        cameraList.setCameraId(id);
        cameraList.setIpAddr(ipAddr);
        cameraList.setIdentifier(identifier);
        cameraList.setPort(port);
        cameraList.setMonitorUrl(monitorUrl);
        cameraList.setLngLat(lngLat);
        cameraList.setSort(sort);
        //执行增加的sql语句
        cameraListDao.insert(cameraList);
    }

    @Override
    public CameraListVO selectById(Integer id) {
        return cameraListDao.selectById(id);
    }

    @Override
    public void update(CameraListVO cameraListVO) {
        if (cameraListVO.getCameraName() == null || cameraListVO.getCameraName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写摄像头名称");
        }

        //通过前端发送的标题名字查询出来对应的id
        CameraType cameraType = cameraTypeDao.selectByName(cameraListVO.getCameraType());
        Integer cameraTypeId = cameraType.getId();

        Integer id = cameraListVO.getId();
        String cameraName = cameraListVO.getCameraName();
        Integer did = cameraListVO.getDid();
        String ipAddr = cameraListVO.getIpAddr();
        String identifier = cameraListVO.getIdentifier();
        String port = cameraListVO.getPort();
        String monitorUrl = cameraListVO.getMonitorUrl();
        BigDecimal lngLat = cameraListVO.getLngLat();
        String sort = cameraListVO.getSort();

        //把相对应的数据()装到一个新实体类对象中
        CameraList cameraList = new CameraList();
        cameraList.setId(id);
        cameraList.setCameraName(cameraName);
        cameraList.setDid(did);
        cameraList.setCameraId(cameraTypeId);
        cameraList.setIpAddr(ipAddr);
        cameraList.setIdentifier(identifier);
        cameraList.setPort(port);
        cameraList.setMonitorUrl(monitorUrl);
        cameraList.setLngLat(lngLat);
        cameraList.setSort(sort);
        //执行更新的sql语句
        cameraListDao.updateById(cameraList);
    }
}
